// 入驻申请
<template>
   <div class="search">
     <Row>
        <Card>
          <Row class="operation">
            <Form inline :label-width="100" ref="searchForm" :model="searchForm" label-position="right">
              <Button @click="handleSearch" icon="md-refresh">刷新</Button>
              <FormItem label="客户手机号" prop="phone">
                <Input v-model="searchForm.phone" placeholder="请输入客户手机号" />
              </FormItem>
              
              <FormItem label="支付开始时间" prop="payTimeBeginTime">
                <Date-picker type="datetime" placeholder="选择日期和时间" style="width: 200px" @on-change="changeTime($event,'begin')"></Date-picker>
              </FormItem>
              <FormItem label="支付结束时间" prop="payTimeEndTime	">
                <Date-picker type="datetime" placeholder="选择日期和时间" style="width: 200px" @on-change="changeTime($event,'end')"></Date-picker>
              </FormItem>
              <FormItem label="订单方式" prop="orderStatus">
                <Select v-model="searchForm.orderStatus" style="width:200%" clearable>
                  <Option value="PRE_PAY">待付款</Option>
                  <Option value="PRE_SEND">待处理</Option>
                  <Option value="DELIVER_SEND">已处理</Option>
                  <Option value="PRE_COMMENT">待评价</Option>
                  <Option value="SALE_AFTER">退款售后</Option>
                  <Option value="CANCEL_ORDER">取消订单</Option>
                  <Option value="IS_COMPLETE">完成订单</Option>
                  <Option value="RETURN_FINISH">退款完成</Option>
                </Select>
              </FormItem>
              <Button @click="handleSearch" type="primary" icon="ios-search" style="margin-left:100px;">搜索</Button>
            </Form>
          </Row>
          <Modal title="查看大图" v-model="visible" class-name="fl-image-modal">
            <img :src="imgUrl" v-if="visible" style="width: 100%">
          </Modal>

          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>

          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
     </Row>

     <Modal :title="modalTitle" v-model="commentVisible" :mask-closable='false' :width="600">
      <Form ref="commentForm" :model="commentForm" :label-width="100">
        <FormItem label="快递单号" prop="expressCode">
          <Input type="text" v-model="commentForm.expressCode" placeholder="请输入快递单号" />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="commentVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
      </div>
    </Modal>

    <Modal :title="modalTitle" v-model="detailVisible" :mask-closable='false' :width="800">
      <div style="padding: 10px 0;font-size: 16px;"><label>商品名称：</label>{{goodsDetail.name}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>商品图片：</label><img :src="goodsDetail.pic" style="width: 200px"/></div>
      <div style="padding: 10px 0;font-size: 16px;"><label>商品类型：</label>{{goodsDetail.shopCartType}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>购买类型：</label>{{goodsDetail.buyType}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>购买数量：</label>{{goodsDetail.buyCount}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>尾款：</label>{{goodsDetail.goodBalancePayment}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>定金：</label>{{goodsDetail.goodDeposit}}元</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>价格：</label>{{goodsDetail.price}}元</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>满减金额：</label>{{goodsDetail.subtractMoney}}元</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>支付方式：</label>{{goodsDetail.payTypeEnum}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>尾款类型：</label>{{goodsDetail.rentType}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>尾款支付类型：</label>{{goodsDetail.rentPayType}}</div>
      <div style="padding: 10px 0;font-size: 16px;">
        <label>规格属性：</label>
        <div v-for="(item,index) in goodsDetail.attrSymbolPath" :key="index">
          <span>规格{{index+1}}：{{item.attrValue}}</span>
        </div>
      </div>
    </Modal>

    <Modal :title="modalTitle" v-model="refundVisible" :mask-closable='false' :width="600">
      <Form ref="refundForm" :model="refundForm" :label-width="100" :rules="refundFormRules">
        <FormItem label="退款方式">
          <Select style="width: 100%" @on-change="changeType">
            <Option value="all">全额退款</Option>
            <Option value="noAll">部分退款</Option>
          </Select>
        </FormItem>
        <FormItem label="退款金额" prop="actualRefundMoney" v-if="refundType === 'noAll'">
          <Input v-model="refundForm.actualRefundMoney" style="width: 100%" placeholder="请输入退款金额" />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="refundVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit2">提交</Button>
      </div>
    </Modal>

    <Modal :title="modalTitle" v-model="refundDetailVisible" :mask-closable='false' :width="800">
      <div style="padding: 10px 0;font-size: 16px;"><label>退款方式：</label>{{refundDetail.type}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>尾款支付方式：</label>{{refundDetail.rentType}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>退款原因：</label>{{refundDetail.returnReason}}</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>退款金额：</label>{{refundDetail.refundMoney}}元</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>实际退款金额：</label>{{refundDetail.actualRefundMoney}}元</div>
      <div style="padding: 10px 0;font-size: 16px;"><label>创建时间：</label>{{refundDetail.createTime}}</div>
    </Modal>
   </div> 
</template>

<script>
export default {
  data(){
    return{ 
      refundDetailVisible: false,
      refundDetail: {},
      refundType: "",
      refundVisible: false,
      refundForm: {
        orderId: "",
        actualRefundMoney: ""
      },
      refundFormRules: {
        actualRefundMoney: [{ required: true, message: "请输入退款金额", trigger: "blur", pattern:/^(([1-9]\d{0,9})|0)(\.\d{0,2})?$/ },
                    { required: true, message: "退款金额格式不正确",pattern:/^(([1-9]\d{0,9})|0)(\.\d{0,2})?$/}],
      },
      submitLoading: false,
      visible:false,
      detailVisible: false,
      total:0,
      modalTitle: "",
      loading: true, // 表单加载状态
      data:[],//表单数据
      searchForm:{
        storeId:"",
        payTimeBeginTime: "",
        payTimeEndTime: "",
        phone: "",
        pageSize:10,
        pageNumber:1,
        orderStatus: ""
      },
      commentForm: {
        orderId: "",
        expressCode: ""
      },
      commentVisible: false,
      columns: [
      // 表头
      {
        type: "index",
        width: 60,
        align: "center"
      },
      {
        title: "订单编号",
        key: "orderNum",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "订单类型",
        key: "typeEnum",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "购买人",
        key: "consignee",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "联系方式",
        key: "phone",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "运费（元）",
        key: "freightPrice",
        minWidth: 100,
        sortable: false,
      },
      {
        title: "订单总金额（元）",
        key: "totalPrice",
        minWidth: 140,
        sortable: false,
      },
      {
        title: "支付方式",
        key: "payTypeEnum",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "尾款支付类型",
        key: "rentPayType",
        minWidth: 120,
        sortable: false,
      },
      // {
      //   title: "收货地址",
      //   key: "address",
      //   minWidth: 120,
      //   sortable: false,
      // },
      {
        title: "订单状态",
        key: "orderStatus",
        minWidth: 120,
        sortable: false,
      },
      {
        title: "创建时间",
        key: "createTime",
        minWidth: 120,
        sortable: false,
      },
      {
          title: "操作",
          width: 260,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.detail(params.row);
                    }
                  }
                },
                "详情"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px",
                    display:(params.row.orderStatus=='待处理')?"inline-block":"none"
                  },
                  on: {
                    click: () => {
                      this.deliverGoods(params.row);
                    }
                  }
                },
                "处理订单"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "default",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px",
                    display:(params.row.orderStatus=='待收款')?"inline-block":"none"
                  },
                  on: {
                    click: () => {
                      this.confirmReceipt(params.row);
                    }
                  }
                },
                "确认收款"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "default",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px",
                    display:(params.row.orderStatus=='退款售后')?"inline-block":"none"
                  },
                  on: {
                    click: () => {
                      this.forcedRefund(params.row);
                    }
                  }
                },
                "退款"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "default",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px",
                    display:(params.row.orderStatus=='退款完成')?"inline-block":"none"
                  },
                  on: {
                    click: () => {
                      this.refundDetails(params.row);
                    }
                  }
                },
                "退款详情"
              ),
            ]);
          }
        }
      ],
      goodsDetail: {},
      }
    },
    methods:{
      init(){
        this.getDataList()
      },
      handleView(url) {
        this.imgUrl = url;
        this.visible = true;
      },
      getDataList(){
        this.getRequest("/ruanyun/user/info").then(res=> {
          if (res.success) {
            this.searchForm.storeId = res.result.id;
            (this.searchForm.orderStatus === undefined) ? (this.searchForm.orderStatus = "") : ""
            this.loading = true;
            this.postRequest("/order/getBackOrderList",this.searchForm).then(res =>{
              this.loading = false
              if(res.success){
                var arr = res.result.dataResult;
                for(let val of arr){
                  for(let i in val){
                    (val[i] === null) ? (val[i] = "-") : ""
                  }
                }
                this.data = arr;
                this.total = res.result.totalPage;
              } else {
                this.data = [];
                this.total = 0;
              }
            })
          }
        })
      },
      //搜索查询
      handleSearch(){
        this.searchForm.pageNumber = 1;
        this.getDataList()
      },
      changeSort(e) {
        this.searchForm.sort = e.key;
        this.searchForm.order = e.order;
        if (e.order === "normal") {
          this.searchForm.order = "";
        }
        this.getDataList();
      },
      changePage(v) {
        this.searchForm.pageNumber = v;
        this.getDataList();
      },
      changePageSize(v) {
        this.searchForm.pageSize = v;
        this.getDataList();
      },
      clearSelectAll(){
        this.$refs.searchForm.resetFields()
        this.getDataList()
      },
      // 发货
      deliverGoods (v) {
        // this.modalTitle = "处理订单";
        // this.commentForm.expressCode = ""
        // this.commentForm.orderId = v.id;
        // this.commentVisible = true;
        this.$Modal.confirm({
          title: "处理订单",
          content: "确认处理该条订单吗？",
          loading: true,
          onOk: ()=> {
            var data = {
              orderId: v.id
            }
            this.postRequest("/order/sendGood",data).then(res=> {
              this.loading = false
              if (res.success) {
                this.$Message.success("操作成功");
                this.getDataList();
                this.$Modal.remove()
              } else {
                this.$Message.success(res.message);
                this.getDataList();
                this.$Modal.remove()
              }
            })
          }
        })
      },
      confirmReceipt (v) {
        this.$Modal.confirm({
          title: "确认",
          content: "确认收款吗？",
          loading: true,
          onOk: ()=> {
            var data = {
              orderId: v.id
            }
            this.postRequest("/order/confirmTheBalance", data).then(res=> {
              this.loading = false
              if (res.success) {
                this.$Message.success("收款成功");
                this.getDataList();
                this.$Modal.remove();
              } else {
                this.$Message.success(res.message);
                this.$Modal.remove();
              }
            })
          }
        })
      },
      changeType (event) {
        this.refundType = event;
      },
      forcedRefund (v) {
        this.modalTitle = "退款";
        this.refundForm.orderId = v.id;
        this.refundVisible = true;
      },
      handleSubmit2 () {
        console.log(this.refundType)
        if (this.refundType === "all") {
          this.refundForm.actualRefundMoney = "";
          this.postRequest("/orderAfterSale/resolveOrderAfterSale", this.refundForm).then(res=> {
            this.submitLoading = false;
            if (res.success) {
              this.$Message.success("退款成功");
              this.getDataList();
              this.refundVisible = false;
            } else {
              this.$Message.error(res.message);
              this.refundVisible = false;
            }
          })
        } else {
          this.$refs.refundForm.validate(valid=> {
            if (valid) {
              this.submitLoading = true
              this.postRequest("/orderAfterSale/resolveOrderAfterSale", this.refundForm).then(res=> {
                this.submitLoading = false;
                if (res.success) {
                  this.$Message.success("退款成功");
                  this.getDataList();
                  this.refundVisible = false;
                } else {
                  this.$Message.error(res.message);
                  this.refundVisible = false;
                }
              })
            }
          })
        }
      },
      handleSubmit () {
        this.$refs.commentForm.validate(valid=> {
          if (valid) {
            this.submitLoading = true;
            this.postRequest("/order/sendGood",this.commentForm).then(res=> {
              this.submitLoading = false
              if (res.success) {
                this.$Message.success("发货成功");
                this.getDataList();
                this.commentVisible = false;
              } else {
                this.$Message.success(res.message);
                this.getDataList();
                this.commentVisible = false;
              }
            })
          }
        })
      },
      // 订单详情 
      detail (v) {
        this.modalTitle = "商品详情";
        var data = {
          id: v.id
        }
        this.postRequest("/order/getMyOrderDetail", data).then(res=> {
          if (res.success) {
            this.goodsDetail = res.result.orderDetailVo;
          } else {
            this.goodsDetail = {}
          }
        })
        // this.goodsDetail = v;
        this.detailVisible = true;
      },
      refundDetails (v) {
        this.modalTitle = "退款详情"
        var data = {
          orderId: v.id
        }
        this.postRequest("/orderAfterSale/appOrderAfterSaleDetail", data).then(res=> {
          if (res.success) {
            this.refundDetail = res.result;
          } else {
            this.refundDetail = {}
          }
        })
        this.refundDetailVisible = true;
      }
    },
    mounted(){
      this.init()
    }
}
</script>

<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>